﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Security.Permissions;
using System.ServiceProcess;
using System.Text;
using System.Threading;

namespace DirectoryWatcherServices
{
    public partial class DirectoryWatcher : ServiceBase
    {
        #region Attributes

        protected static string svnpath = ConfigurationManager.AppSettings["SVNPATH"].ToString();
        protected static string[] repos = ConfigurationManager.AppSettings["REPOS"].ToString().Split(';');
        protected static string[] repospath = ConfigurationManager.AppSettings["REPOSPATH"].ToString().Split(';');
        protected static string[] codepath = ConfigurationManager.AppSettings["CODEPATH"].ToString().Split(';');
        protected static string logpath = ConfigurationManager.AppSettings["LOGPATH"].ToString();


        #endregion

        [PermissionSet(SecurityAction.Demand,Name="FullTrust")]
        public DirectoryWatcher()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            Thread[] Thread = new Thread[repos.Length];

            for (int i = 0; i < repos.Length; i++)
            {
                Watcher Watcher = new Watcher(repos[i], repospath[i], codepath[i]);
                Thread[i] = new Thread(new ThreadStart(Watcher.Start));
                Thread[i].Start();
            } 
        }

        protected override void OnStop()
        {
        }
    }

    public class Watcher
    {
        #region Attributes

        string t_Directory = "";
        string t_Repos = "";
        string t_Code = "";

        protected static string logpath = ConfigurationManager.AppSettings["LOGPATH"].ToString();

        delegate void DeleGate(string txt);

        #endregion

        public Watcher()
        {
        }

        public Watcher(string Dir, string Rep, string Cod)
        {
            t_Directory = Dir;
            t_Repos = Rep;
            t_Code = Cod;
        }

        public void Start()
        {
            FileStream fs;
            StreamWriter txt;
            if (!Directory.Exists(t_Directory))
            {
                fs = new FileStream(logpath, FileMode.Append, FileAccess.Write, FileShare.Read);
                txt = new StreamWriter(fs);
                txt.WriteLine(DateTime.Now.ToString() + "监控点：" + t_Directory + "不存在");
                txt.Close(); 
                return;
            }

            WatcherThread();

            fs = new FileStream(logpath, FileMode.Append, FileAccess.Write, FileShare.Read);
            txt = new StreamWriter(fs);
            txt.WriteLine(DateTime.Now.ToString() + ":开始监控文件夹\"" + t_Directory + "\"");
            txt.Close();
        }

        protected void WatcherThread()
        {
            FileSystemWatcher Watcher = new FileSystemWatcher();
            Watcher.Path = t_Directory;
            Watcher.NotifyFilter = NotifyFilters.Attributes | NotifyFilters.DirectoryName | NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.Size;

            Watcher.Changed += new FileSystemEventHandler(OnChanged);
            Watcher.Created += new FileSystemEventHandler(OnChanged);
            Watcher.Deleted += new FileSystemEventHandler(OnChanged);
            Watcher.Renamed += new RenamedEventHandler(OnChanged);

            Watcher.EnableRaisingEvents = true;
        }

        protected void OnChanged(object source, FileSystemEventArgs e)
        {
            FileStream fs = new FileStream(logpath, FileMode.Append, FileAccess.Write, FileShare.Read);
            StreamWriter txt = new StreamWriter(fs);
            txt.WriteLine(DateTime.Now.ToLongTimeString() + ":" + e.FullPath + " " + e.ChangeType);
            txt.Close();
        }
    }
}
